﻿IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'userFindInRole')
	BEGIN
		DROP  Procedure  userFindInRole
	END
GO

-- =============================================
-- Author:		Ricardo Sosa
-- Create date: 03/04/2009
-- Description:	Finds the users that match the search criteria and belong to the specified role
-- =============================================
CREATE PROCEDURE [dbo].[userFindInRole]
	@role_name VARCHAR(50),
	@user_match VARCHAR(50),
	@app_id INT
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	DECLARE @role_id INT;

	DECLARE @full_match VARCHAR(52)
	SET @full_match = '%' + @user_match + '%';

	SELECT @role_id = lit_rol_id
	FROM lit_rol
	WHERE lit_rol_nombre = @role_name
	AND lit_aplicacion_id = @app_id;
	
    SELECT * 
	FROM lit_usuario
	WHERE lit_usuario_id IN (SELECT lit_usuario_id 
							FROM lit_usuario_rol 
							WHERE lit_rol_id = @role_id)
	AND lit_usuario_username like @full_match
	ORDER BY lit_usuario_username;

END
GO